﻿#include "OiPCH.hpp"
#include "View/OiPixmap.hpp"
namespace Oi
{
    namespace PLScannerCore
    {

        void FlipData::flip(bool x)
        {
            if (x)
            {
                auto pixmap = ImageViewer::OiImageViewer()->pixmap()->pixmap();
                auto pointMap = ImageViewer::OiImageViewer()->pointMap();
                auto rangeMap = ImageViewer::OiImageViewer()->rangeMap();
                auto pointMat = ImageViewer::OiImageViewer()->pointRemapMat();
                if (!pointMap.isNull() && !pointMat.empty())
                {
                    auto point1 = Core::PointMap::create(pointMap->cols(), pointMap->rows());
                    point1->resize(pointMap->rows());
                    for (int i = 0; i < pointMap->rows(); i++)
                    {
                        auto pz = pointMap->zLine(i);
                        auto px = pointMap->xLine(i);
                        auto py = pointMap->yLine(i);
                        auto pz1 = point1->zLine(pointMap->rows()-1-i);
                        auto px1 = point1->xLine(pointMap->rows() - 1 - i);
                        auto py1 = point1->yLine(pointMap->rows() - 1 - i);

                        /*for (int j = 0; j < pointMap->cols(); j++)
                        {
                        pz1[j] = pz[j];
                        px1[j] = px[j];
                        py1[j] = py[j];
                        }*/
                        memcpy(pz1, pz, pointMap->cols() * sizeof(float));
                        memcpy(px1, px, pointMap->cols() * sizeof(float));
                        memcpy(py1, py, pointMap->cols() * sizeof(float));

                    }

                    ImageViewer::OiImageViewer()->display(point1);
                }
                else if (!rangeMap.isNull())
                {
                    
                    auto range1 = Core::RangeMap::create(rangeMap->cols(), rangeMap->rows());
                    for (int i = 0; i < rangeMap->rows(); ++i)
                    {
                        auto ran = rangeMap->profile(rangeMap->rows()-1-i);
                        auto ran1 = range1->newProfile();
                        memcpy(ran1, ran, rangeMap->cols() * sizeof(float));

                    }


                    ImageViewer::OiImageViewer()->display(range1);
                }
                else if (!pixmap.isNull())
                {
                    QImage im = pixmap.mirrored(false,true);
                    ImageViewer::OiImageViewer()->display(im);
                }
                else
                {
                    return;
                }
                ImageViewer::OiImageViewer()->zoomFit();
            }
            else {
                //竖直翻转
                auto pixmap = ImageViewer::OiImageViewer()->pixmap()->pixmap();
                auto pointMap = ImageViewer::OiImageViewer()->pointMap();
                auto rangeMap = ImageViewer::OiImageViewer()->rangeMap();
                auto pointMat = ImageViewer::OiImageViewer()->pointRemapMat();
                if (!pointMap.isNull() && !pointMat.empty())
                {
                    auto point1 = Core::PointMap::create(pointMap->cols(), pointMap->rows());
                    point1->resize(pointMap->rows());
                    for (int i = 0; i < pointMap->rows(); i++)
                    {
                        auto pz = pointMap->zLine(i);
                        auto px = pointMap->xLine(i);
                        auto py = pointMap->yLine(i);
                        auto pz1 = point1->zLine(i);
                        auto px1 = point1->xLine(i);
                        auto py1 = point1->yLine(i);

                        memcpy(pz1, pz, pointMap->cols() * sizeof(float));
                        memcpy(py1, py, pointMap->cols() * sizeof(float));
                        for (int j = 0; j < pointMap->cols(); ++j)
                        {
                            px1[j] = -px[j];
                        }

                    }

                    ImageViewer::OiImageViewer()->display(point1);
                }
                else if (!rangeMap.isNull())
                {

                    auto range1 = Core::RangeMap::create(rangeMap->cols(), rangeMap->rows());
                    for (int i = 0; i < rangeMap->rows(); ++i)
                    {
                        auto ran = rangeMap->profile(i);
                        auto ran1 = range1->newProfile();
                        for (int j = 0; j < rangeMap->cols(); ++j)
                        {
                            ran1[j] = ran[rangeMap->cols()-1-j];
                        }
                    }


                    ImageViewer::OiImageViewer()->display(range1);
                }
                else if (!pixmap.isNull())
                {
                    QImage im = pixmap.mirrored( true, false);
                    ImageViewer::OiImageViewer()->display(im);
                }
                else
                {
                    return;
                }
                ImageViewer::OiImageViewer()->zoomFit();
            }
        }

    }
}

